package com.breaking.second.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.breaking.second.R;
import com.breaking.second.activity.pager.adapter.GoodsAdapter;
import com.breaking.second.databinding.ActivityMyGoodsBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.GoodsEntity;
import com.breaking.second.entity.PageableEntity;
import com.breaking.second.recycler.RecyclerItemClickListener;
import com.breaking.second.utils.compat.FormBodyCompat;
import com.breaking.second.utils.compat.RefreshLayoutCompat;
import com.breaking.second.utils.compat.SkeltonCompat;
import com.breaking.second.utils.okhttp.OkhttpUtil;
import com.ethanhua.skeleton.ViewSkeletonScreen;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;

public class MyGoodsActivity extends BaseActivity<ActivityMyGoodsBinding> {
    private final List<GoodsEntity> goodsEntities = new ArrayList<>();
    private GoodsAdapter adapter;
    private ViewSkeletonScreen d;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initSmart();
        initRecycler();
        initSkelton();
    }

    private void initSkelton() {
        d = SkeltonCompat.bind(binding.smart, R.layout.layout_empty).show();
    }

    private void initRecycler() {
        adapter = new GoodsAdapter(this, goodsEntities);
        binding.recycler.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler.setAdapter(adapter);
    }


    private void initSmart() {
        RefreshLayoutCompat.setRefresh(pageableVo, binding.smart, this::onRefresh);
    }

    private void onRefresh(RefreshLayout refreshLayout) {
        getGoodsData();
    }

    private void initData() {
        getGoodsData();
    }

    private void getGoodsData() {
        FormBody.Builder form = FormBodyCompat.to(pageableVo);
        OkhttpUtil.Get("goods/myself", form, new OkhttpUtil.CallbackListener(this::getGoodsData),binding.smart);
    }

    private void getGoodsData(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            PageableEntity pageable = bookEntity.to(PageableEntity.class);

            if (pageable == null) return;
            if (pageable.getSize() != pageable.getSize()) {//禁止加载更多
                binding.smart.finishLoadMoreWithNoMoreData();
            }

            List<GoodsEntity> goodsEntities = pageable.toList(GoodsEntity.class);
            if (goodsEntities == null) return;
            if (goodsEntities.isEmpty()) return;
            if (pageable.getCurrent() == 0) {
                adapter.notifyItemRangeRemoved(0, this.goodsEntities.size() + 1);
                this.goodsEntities.clear();
            }

            int start = this.goodsEntities.size();
            this.goodsEntities.addAll(goodsEntities);
            adapter.notifyItemRangeChanged(start, this.goodsEntities.size());

            if (!this.goodsEntities.isEmpty()) {
                d.hide();
            }
        }
    }
}
